using System;
using System.Collections.Generic;
using System.Data;
using MetaObjects.Contracts;

namespace MetaObjects.Core.Tests
{
	public class TestFactory
	{
		public static readonly DataTable Table = CreateTable();

		public static TestEntity CreateEntity()
		{
			return new TestEntity
       		{
	       		Id = 1,
				Title = "TestEntity1",
				Description = "TestEntityDescription1",
				Address = new TestValueObject(),
				Created = DateTime.Now,
				Modified = null
       		};
		}

		public static IEnumerable<TestEntity> CreateEntityCollection(int count)
		{
			for (int i = 0; i < count; i++)
			{
				yield return new TestEntity
				{
					Id = i,
					Title = "TestEntity" + i,
					Description = "TestEntityDescription" + i,
					Address = new TestValueObject(),
					Created = DateTime.Now,
					Modified = null
				};
			}
		}

		public static DataTable CreateTable()
		{
			DataTable table = new DataTable();
			table.TableName = "TestDataTable";
			table.Columns.Add(new DataColumn("Id", typeof(int)));
			table.Columns.Add(new DataColumn("Title", typeof(string)));
			table.Columns.Add(new DataColumn("Address", typeof(TestValueObject)));
			table.Columns.Add(new DataColumn("Description", typeof(string)));
			table.Columns.Add(new DataColumn("Created", typeof(DateTime)));
			table.Columns.Add(new DataColumn("Modified", typeof(DateTime)));
			return table;
		}

		public static DataRow CreateRow()
		{
			DataRow row = CreateTable().NewRow();
			row["Id"] = 1;
			row["Title"] = "TestRow";
			row["Address"] = new TestValueObject();
			row["Description"] = "TestRowDescription";
			row["Created"] = DateTime.Now;
			row["Modified"] = DBNull.Value;
			return row;
		}

		public static IEnumerable<DataRow> CreateRowCollection(int count)
		{
			for (int i = 0; i < count; i++)
			{
				DataRow row = CreateTable().NewRow();
				row["Id"] = 1;
				row["Title"] = "TestRow";
				row["Address"] = new TestValueObject();
				row["Description"] = "TestRowDescription";
				row["Created"] = DateTime.Now;
				row["Modified"] = DBNull.Value;
				yield return row;
			}
		}

		public static DataRow TestEntityToRowMap(TestEntity entity)
		{
			DataRow row = Table.NewRow();
			row["Id"] = entity.Id;
			row["Title"] = entity.Title;
			row["Address"] = entity.Address;
			row["Description"] = entity.Description;
			row["Created"] = entity.Created ?? (object)DBNull.Value;
			row["Modified"] = entity.Modified ?? (object)DBNull.Value;
			return row;
		}

		public static TestEntity TestRowToEntityMap(DataRow row)
		{
			TestEntity entity = new TestEntity();
			entity.Id = (int)row["Id"];
			entity.Title = (string)row["Title"];
			entity.Address = (TestValueObject)row["Address"];
			entity.Description = (string)row["Description"];
			entity.Created = (row["Created"] == DBNull.Value) ? null : (DateTime?)row["Created"];
			entity.Modified = (row["Modified"] == DBNull.Value) ? null : (DateTime?)row["Modified"];
			return entity;
		}

		public static IMapper<DataRow, TestEntity> CreateMapper<T, T1>(DataTable table, Type type)
		{
			throw new NotImplementedException();
		}
	}
}